/** 
  Copyright (c) 2008, 2009 Claudio Imbrenda
  This program is released under the terms of the
  GNU GPL version 3 or later.
*/
package interfaccia;


import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import macchina.*;

import static macchina.MRUtils.*;

/**
 * 
 * @author Claudio Imbrenda
 */
public class MRUberPanel extends javax.swing.JPanel implements MRPanelInterface{
  
  
  /**
   * A list of ActionListeners listening to the events generated by this
   * instance of MRUberPanel.
   */
  private java.util.List<ActionListener> ascoltatori = new ArrayList<ActionListener>();
  
  private static ResourceBundle bundle_interfaccia = 
          ResourceBundle.getBundle("interfaccia/Bundle_interfaccia");
  
  /**
   * The instance of Macchina to handle.
   */
  private Macchina m;
  
  /**
   * The TableModel used to represent the memory.
   */
  private AbstractTableModel model;
  
  /**
   * Contains the type of the memory cells.
   */
  private int coltypes[]=new int[1024];
  
  private int rcolors[] = {-1,-1,-1,-1,-1,-1};
  
  /**
   * The address of the currently selected memory location.
   */
  private short curselected=NOME_REG_A;
  
  private boolean showsidebar = true;
  
  private int refresh = 1000;
  
  /**
   * The background colors of the cells, indexed by type.
   */
  private static final Color colori[] = {
    new Color(255, 200, 200),
    new Color(255, 255, 200),
    new Color(200, 255, 200),
    new Color(160, 192, 255),
    new Color(200, 255, 255),
    new Color(255, 200, 255),
    new Color(128,96,192)
  };

  
  private static final String[] tipi = {
    bundle_interfaccia.getString("Istruzioni"),
    bundle_interfaccia.getString("Virgola_Fissa"),
    bundle_interfaccia.getString("Binario"),
    bundle_interfaccia.getString("Interi"),
    bundle_interfaccia.getString("Esadecimale"),
    bundle_interfaccia.getString("Caratteri"),
    bundle_interfaccia.getString("Interi_con_segno"),
  };
  
  /** Creates new form MRUberPanel 
   *
   * @param mm the instance of Macchina to be handled by this instance of
   * MRUberPanel.
   */
  public MRUberPanel(Macchina mm) {
    initComponents();
    
    lblMouseLabel.setVisible(false);
    lblSelectedLabel.setVisible(false);
    
    m=mm;
    //jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    _updatedefaulttype();
    
    for(int cx=0;cx<1024;cx++)coltypes[cx]=-1;            
    
    _mktablemodel(jTable1,10);
    
    ListSelectionListener lis = new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {
        int c = jTable1.getSelectedColumn();
        int r = jTable1.getSelectedRow();
        int nc= jTable1.getColumnCount();
        if((c<0)||(r<0))curselected=NOME_REG_A;
        else curselected=(short)(r*nc+c);
        _updatecurselected();
      }
    };
    
    comboDefType.setModel(new DefaultComboBoxModel(tipi));
    
    jTable1.getSelectionModel().addListSelectionListener(lis);
    jTable1.getColumnModel().getSelectionModel().addListSelectionListener(lis);
              
    updateMEM();
    _updatecurselected();
    _hideinfo();
  }
  
  
  
  public void updateUI(){
      super.updateUI();
      if(jPopupMenu1!=null)
          SwingUtilities.updateComponentTreeUI(jPopupMenu1);
  }
  
  /**
   * Creates a AbstractTableModel for the given, with the given number of
   * columns, and an appropriate TableCellRenderer.
   * @param table the table to apply the model to.
   * @param ncols the number of columns.
   * @return the new AbstractTableModel.
   */
  private void _mktablemodel(JTable table, final int NCOL){
      table.setModel(model=new AbstractTableModel() {
        public int getRowCount(){return 1024/NCOL+(1024%NCOL==0?0:1);}
        public int getColumnCount(){return NCOL;}
        public Object getValueAt(int r, int c) {
            short a = (short)(c+r*NCOL);
            if(a<1024)return niceformat(m.getMEM(a),m,
                  a==m.getN()?0:(coltypes[a]==-1)?modovisualizza:coltypes[a]);
            else
                return null;
        }
        public String getColumnName(int idx){return Integer.toHexString(idx);}
        public void setValueAt(Object o, int r, int c){
            short a = (short)(c+r*NCOL);
        
            if(a<1024)
                m.setMEM(a,understand(o.toString(),m,m.getMEM(a)));
            _updatecurselected();
        }
        public boolean isCellEditable(int r,int c){return (c+r*NCOL)<1024;}
      });
    
      TableCellRenderer cr = new DefaultTableCellRenderer(){
        public Component getTableCellRendererComponent(JTable table,
              Object v, boolean se, boolean foc,int r, int c) {
            int a = c+r*table.getColumnCount();
            if(a>=1024)setBackground(table.getGridColor());
            else if(coltypes[a]==-1)setBackground(null);
            else setBackground(colori[coltypes[a]%colori.length]);
            if(a==m.getN())setBackground(Color.RED);
            return super.getTableCellRendererComponent(table,v,se,foc,r,c);
        }
      };
    
      for(int cx=0;cx<jTable1.getColumnCount();cx++)
        jTable1.getColumnModel().getColumn(cx).setCellRenderer(cr);
  }
  
  
  /**
   * Fires an event to the listeners.
   * @param what the type of the event to fire.
   */
  private void fireChange(int what){
    final ActionEvent ae=new ActionEvent(this,what,"");
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        for(ActionListener a:ascoltatori)
        a.actionPerformed(ae);
      }
    });
  }
  
  /**
   * Updates the memory display according to the selelected default memory type.
   */
  private void _updatedefaulttype(){
    modovisualizza=comboDefType.getSelectedIndex()%widtharr.length;
    for(int cx=0;cx<jTable1.getColumnCount();cx++)
      jTable1.getColumnModel().getColumn(cx)
                .setPreferredWidth(widtharr[modovisualizza]);
    updateMEM();
 }
 
  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    buttonGroup1 = new javax.swing.ButtonGroup();
    jPopupMenu1 = new javax.swing.JPopupMenu();
    jMenuItem1 = new javax.swing.JMenuItem();
    jSeparator8 = new javax.swing.JSeparator();
    jMenuItem2 = new javax.swing.JMenuItem();
    jMenuItem3 = new javax.swing.JMenuItem();
    jMenuItem4 = new javax.swing.JMenuItem();
    jMenuItem5 = new javax.swing.JMenuItem();
    jMenuItem6 = new javax.swing.JMenuItem();
    jMenuItem7 = new javax.swing.JMenuItem();
    jMenuItem13 = new javax.swing.JMenuItem();
    jMenuItem8 = new javax.swing.JMenuItem();
    jSeparator2 = new javax.swing.JSeparator();
    jMenuItem9 = new javax.swing.JMenuItem();
    jMenuItem10 = new javax.swing.JMenuItem();
    jSeparator9 = new javax.swing.JSeparator();
    jMenu2 = new javax.swing.JMenu();
    jMenuItem11 = new javax.swing.JMenuItem();
    jMenuItem12 = new javax.swing.JMenuItem();
    jSeparator7 = new javax.swing.JSeparator();
    jSeparator3 = new javax.swing.JSeparator();
    jMenu1 = new javax.swing.JMenu();
    jRadioButtonMenuItem1 = new javax.swing.JRadioButtonMenuItem();
    jRadioButtonMenuItem2 = new javax.swing.JRadioButtonMenuItem();
    jRadioButtonMenuItem3 = new javax.swing.JRadioButtonMenuItem();
    jRadioButtonMenuItem4 = new javax.swing.JRadioButtonMenuItem();
    jRadioButtonMenuItem5 = new javax.swing.JRadioButtonMenuItem();
    jCheckBoxMenuItem1 = new javax.swing.JCheckBoxMenuItem();
    ncolsButtonGroup = new javax.swing.ButtonGroup();
    jPanel5 = new javax.swing.JPanel();
    jPanel12 = new javax.swing.JPanel();
    rdAuto = new javax.swing.JRadioButton();
    rdManualInstr = new javax.swing.JRadioButton();
    rdManualMicroinstr = new javax.swing.JRadioButton();
    chkRunning = new javax.swing.JCheckBox();
    chkAC = new javax.swing.JCheckBox();
    chkExternal = new javax.swing.JCheckBox();
    btnRun = new javax.swing.JButton();
    comboDefType = new javax.swing.JComboBox();
    jLabel1 = new javax.swing.JLabel();
    txtA = new javax.swing.JTextField();
    jLabel2 = new javax.swing.JLabel();
    txtB = new javax.swing.JTextField();
    jLabel3 = new javax.swing.JLabel();
    txtN = new javax.swing.JTextField();
    jLabel4 = new javax.swing.JLabel();
    txtTM = new javax.swing.JTextField();
    jLabel5 = new javax.swing.JLabel();
    txtTI = new javax.swing.JTextField();
    jLabel6 = new javax.swing.JLabel();
    txtVI = new javax.swing.JTextField();
    jSeparator1 = new javax.swing.JSeparator();
    jSeparator5 = new javax.swing.JSeparator();
    jSeparator4 = new javax.swing.JSeparator();
    jSeparator6 = new javax.swing.JSeparator();
    lblClockCounter = new javax.swing.JLabel();
    lblClockLabel = new javax.swing.JLabel();
    jPanel4 = new javax.swing.JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTable1 = new javax.swing.JTable();
    jPanel23 = new javax.swing.JPanel();
    lblSelectedLabel = new javax.swing.JLabel();
    lblSelected = new javax.swing.JLabel();
    jSeparator10 = new javax.swing.JSeparator();
    lblMouseLabel = new javax.swing.JLabel();
    lblMouse = new javax.swing.JLabel();

    jMenuItem1.setText("Item");
    jMenuItem1.setEnabled(false);
    jPopupMenu1.add(jMenuItem1);
    jPopupMenu1.add(jSeparator8);

    jMenuItem2.setForeground(new java.awt.Color(255, 0, 0));
    jMenuItem2.setText(bundle_interfaccia.getString("Tipo:_Istruzione")); // NOI18N
    jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem2ActionPerformed(evt);
      }
    });
    jPopupMenu1.add(jMenuItem2);

    jMenuItem3.setForeground(new java.awt.Color(255, 204, 0));
    jMenuItem3.setText(bundle_interfaccia.getString("Tipo:_Virgola_fissa")); // NOI18N
    jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem3ActionPerformed(evt);
      }
    });
    jPopupMenu1.add(jMenuItem3);

    jMenuItem4.setForeground(new java.awt.Color(51, 204, 0));
    jMenuItem4.setText(bundle_interfaccia.getString("Tipo:_Base_2")); // NOI18N
    jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem4ActionPerformed(evt);
      }
    });
    jPopupMenu1.add(jMenuItem4);

    jMenuItem5.setForeground(new java.awt.Color(0, 102, 255));
    jMenuItem5.setText(bundle_interfaccia.getString("Tipo:_Intero")); // NOI18N
    jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem5ActionPerformed(evt);
      }
    });
    jPopupMenu1.add(jMenuItem5);

    jMenuItem6.setForeground(new java.awt.Color(0, 204, 204));
    jMenuItem6.setText(bundle_interfaccia.getString("Tipo:_Esadecimale")); // NOI18N
    jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem6ActionPerformed(evt);
      }
    });
    jPopupMenu1.add(jMenuItem6);

    jMenuItem7.setForeground(new java.awt.Color(255, 0, 255));
    jMenuItem7.setText(bundle_interfaccia.getString("Tipo:_Carattere")); // NOI18N
    jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem7ActionPerformed(evt);
      }
    });
    jPopupMenu1.add(jMenuItem7);

    jMenuItem13.setForeground(new java.awt.Color(72, 0, 255));
    java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("interfaccia/Bundle_interfaccia"); // NOI18N
    jMenuItem13.setText(bundle.getString("Tipo:_Interi_con_segno")); // NOI18N
    jMenuItem13.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem13ActionPerformed(evt);
      }
    });
    jPopupMenu1.add(jMenuItem13);

    jMenuItem8.setText(bundle_interfaccia.getString("(tipo_default)")); // NOI18N
    jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem8ActionPerformed(evt);
      }
    });
    jPopupMenu1.add(jMenuItem8);
    jPopupMenu1.add(jSeparator2);

    jMenuItem9.setText(bundle_interfaccia.getString("Imposta_N_qui")); // NOI18N
    jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem9ActionPerformed(evt);
      }
    });
    jPopupMenu1.add(jMenuItem9);

    jMenuItem10.setText(bundle_interfaccia.getString("Vai_a...")); // NOI18N
    jMenuItem10.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem10ActionPerformed(evt);
      }
    });
    jPopupMenu1.add(jMenuItem10);
    jPopupMenu1.add(jSeparator9);

    jMenu2.setText(bundle_interfaccia.getString("Celle_selezionate")); // NOI18N

    jMenuItem11.setText(bundle_interfaccia.getString("Azzera")); // NOI18N
    jMenuItem11.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem11ActionPerformed(evt);
      }
    });
    jMenu2.add(jMenuItem11);

    jMenuItem12.setText(bundle_interfaccia.getString("Imposta_valore...")); // NOI18N
    jMenuItem12.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jMenuItem12ActionPerformed(evt);
      }
    });
    jMenu2.add(jMenuItem12);

    jPopupMenu1.add(jMenu2);
    jPopupMenu1.add(jSeparator7);
    jPopupMenu1.add(jSeparator3);

    jMenu1.setText(bundle_interfaccia.getString("Numero_di_colonne")); // NOI18N

    ncolsButtonGroup.add(jRadioButtonMenuItem1);
    jRadioButtonMenuItem1.setText("8");
    jRadioButtonMenuItem1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jRadioButtonMenuItem1ActionPerformed(evt);
      }
    });
    jMenu1.add(jRadioButtonMenuItem1);

    ncolsButtonGroup.add(jRadioButtonMenuItem2);
    jRadioButtonMenuItem2.setSelected(true);
    jRadioButtonMenuItem2.setText("10");
    jRadioButtonMenuItem2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jRadioButtonMenuItem2ActionPerformed(evt);
      }
    });
    jMenu1.add(jRadioButtonMenuItem2);

    ncolsButtonGroup.add(jRadioButtonMenuItem3);
    jRadioButtonMenuItem3.setText("16");
    jRadioButtonMenuItem3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jRadioButtonMenuItem3ActionPerformed(evt);
      }
    });
    jMenu1.add(jRadioButtonMenuItem3);

    ncolsButtonGroup.add(jRadioButtonMenuItem4);
    jRadioButtonMenuItem4.setText("20");
    jRadioButtonMenuItem4.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jRadioButtonMenuItem4ActionPerformed(evt);
      }
    });
    jMenu1.add(jRadioButtonMenuItem4);

    ncolsButtonGroup.add(jRadioButtonMenuItem5);
    jRadioButtonMenuItem5.setText("32");
    jRadioButtonMenuItem5.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jRadioButtonMenuItem5ActionPerformed(evt);
      }
    });
    jMenu1.add(jRadioButtonMenuItem5);

    jPopupMenu1.add(jMenu1);

    jCheckBoxMenuItem1.setSelected(true);
    jCheckBoxMenuItem1.setText(bundle_interfaccia.getString("Mostra_barra_laterale")); // NOI18N
    jCheckBoxMenuItem1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jCheckBoxMenuItem1ActionPerformed(evt);
      }
    });
    jPopupMenu1.add(jCheckBoxMenuItem1);

    setLayout(new java.awt.BorderLayout());

    jPanel5.setLayout(new java.awt.BorderLayout());

    jPanel12.setLayout(new java.awt.GridBagLayout());

    buttonGroup1.add(rdAuto);
    rdAuto.setText(bundle_interfaccia.getString("Automatica")); // NOI18N
    rdAuto.setToolTipText(bundle_interfaccia.getString("Automatica")); // NOI18N
    rdAuto.setMaximumSize(null);
    rdAuto.setMinimumSize(null);
    rdAuto.setPreferredSize(null);
    rdAuto.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        rdAutoActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
    jPanel12.add(rdAuto, gridBagConstraints);

    buttonGroup1.add(rdManualInstr);
    rdManualInstr.setSelected(true);
    rdManualInstr.setText(bundle_interfaccia.getString("Manuale_Istruzioni")); // NOI18N
    rdManualInstr.setToolTipText(bundle_interfaccia.getString("Manuale_Istruzioni")); // NOI18N
    rdManualInstr.setMaximumSize(null);
    rdManualInstr.setMinimumSize(null);
    rdManualInstr.setPreferredSize(null);
    rdManualInstr.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        rdManualInstrActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
    jPanel12.add(rdManualInstr, gridBagConstraints);

    buttonGroup1.add(rdManualMicroinstr);
    rdManualMicroinstr.setText(bundle_interfaccia.getString("Manuale_Microistruzioni")); // NOI18N
    rdManualMicroinstr.setToolTipText(bundle_interfaccia.getString("Manuale_Microistruzioni")); // NOI18N
    rdManualMicroinstr.setMaximumSize(null);
    rdManualMicroinstr.setMinimumSize(null);
    rdManualMicroinstr.setPreferredSize(null);
    rdManualMicroinstr.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        rdManualMicroinstrActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 4;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
    jPanel12.add(rdManualMicroinstr, gridBagConstraints);

    chkRunning.setText(bundle_interfaccia.getString("Esecuzione")); // NOI18N
    chkRunning.setToolTipText(bundle_interfaccia.getString("Indicatore_di_esecuzione")); // NOI18N
    chkRunning.setEnabled(false);
    chkRunning.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
    chkRunning.setPreferredSize(null);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    jPanel12.add(chkRunning, gridBagConstraints);

    chkAC.setText(bundle_interfaccia.getString("Arresto_Condizionale")); // NOI18N
    chkAC.setToolTipText(bundle_interfaccia.getString("Arresto_Condizionale")); // NOI18N
    chkAC.setMaximumSize(null);
    chkAC.setMinimumSize(null);
    chkAC.setPreferredSize(null);
    chkAC.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        chkACActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
    jPanel12.add(chkAC, gridBagConstraints);

    chkExternal.setText(bundle_interfaccia.getString("Istruzioni_Esterne")); // NOI18N
    chkExternal.setToolTipText(bundle_interfaccia.getString("Istruzioni_Esterne")); // NOI18N
    chkExternal.setMaximumSize(null);
    chkExternal.setMinimumSize(null);
    chkExternal.setPreferredSize(null);
    chkExternal.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        chkExternalActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
    jPanel12.add(chkExternal, gridBagConstraints);

    btnRun.setText(bundle_interfaccia.getString("Esegui")); // NOI18N
    btnRun.setToolTipText("Esegui");
    btnRun.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnRunActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 4;
    gridBagConstraints.insets = new java.awt.Insets(0, 0, 1, 0);
    jPanel12.add(btnRun, gridBagConstraints);

    comboDefType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Istruzioni", "Virgola Fissa", "Binario", "Interi", "Esadecimale", "Caratteri" }));
    comboDefType.setToolTipText(bundle_interfaccia.getString("Scelta_visualizzazione_default")); // NOI18N
    comboDefType.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        comboDefTypeActionPerformed(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 4;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.gridwidth = 2;
    gridBagConstraints.insets = new java.awt.Insets(0, 1, 0, 2);
    jPanel12.add(comboDefType, gridBagConstraints);

    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
    jLabel1.setLabelFor(txtA);
    jLabel1.setText("A");
    jLabel1.setMaximumSize(null);
    jLabel1.setMinimumSize(null);
    jLabel1.setPreferredSize(null);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 4;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.insets = new java.awt.Insets(0, 1, 0, 1);
    jPanel12.add(jLabel1, gridBagConstraints);

    txtA.setText("RegistroA");
    txtA.setToolTipText(bundle_interfaccia.getString("Registro_A")); // NOI18N
    txtA.setMaximumSize(null);
    txtA.setMinimumSize(null);
    txtA.setPreferredSize(null);
    txtA.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        txtAMousePressed(evt);
      }
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        txtAMouseReleased(evt);
      }
      public void mouseExited(java.awt.event.MouseEvent evt) {
        txtAMouseExited(evt);
      }
      public void mouseEntered(java.awt.event.MouseEvent evt) {
        txtAMouseEntered(evt);
      }
    });
    txtA.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        txtAActionPerformed(evt);
      }
    });
    txtA.addFocusListener(new java.awt.event.FocusAdapter() {
      public void focusGained(java.awt.event.FocusEvent evt) {
        txtAFocusGained(evt);
      }
      public void focusLost(java.awt.event.FocusEvent evt) {
        txtAFocusLost(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 5;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    gridBagConstraints.insets = new java.awt.Insets(0, 1, 0, 1);
    jPanel12.add(txtA, gridBagConstraints);

    jLabel2.setLabelFor(txtB);
    jLabel2.setText("B");
    jLabel2.setMaximumSize(null);
    jLabel2.setMinimumSize(null);
    jLabel2.setPreferredSize(null);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 4;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.insets = new java.awt.Insets(0, 1, 0, 1);
    jPanel12.add(jLabel2, gridBagConstraints);

    txtB.setText("RegistroB");
    txtB.setToolTipText(bundle_interfaccia.getString("Registro_B")); // NOI18N
    txtB.setMaximumSize(null);
    txtB.setMinimumSize(null);
    txtB.setPreferredSize(null);
    txtB.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        txtBMousePressed(evt);
      }
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        txtBMouseReleased(evt);
      }
      public void mouseExited(java.awt.event.MouseEvent evt) {
        txtAMouseExited(evt);
      }
      public void mouseEntered(java.awt.event.MouseEvent evt) {
        txtBMouseEntered(evt);
      }
    });
    txtB.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        txtBActionPerformed(evt);
      }
    });
    txtB.addFocusListener(new java.awt.event.FocusAdapter() {
      public void focusGained(java.awt.event.FocusEvent evt) {
        txtBFocusGained(evt);
      }
      public void focusLost(java.awt.event.FocusEvent evt) {
        txtBFocusLost(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 5;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    gridBagConstraints.insets = new java.awt.Insets(0, 1, 0, 1);
    jPanel12.add(txtB, gridBagConstraints);

    jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
    jLabel3.setLabelFor(txtN);
    jLabel3.setText("N");
    jLabel3.setMaximumSize(null);
    jLabel3.setMinimumSize(null);
    jLabel3.setPreferredSize(null);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 4;
    gridBagConstraints.gridy = 4;
    gridBagConstraints.insets = new java.awt.Insets(0, 1, 0, 1);
    jPanel12.add(jLabel3, gridBagConstraints);

    txtN.setText("RegistroN");
    txtN.setToolTipText(bundle_interfaccia.getString("Registro_N")); // NOI18N
    txtN.setMaximumSize(null);
    txtN.setMinimumSize(null);
    txtN.setPreferredSize(null);
    txtN.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        txtNMousePressed(evt);
      }
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        txtNMouseReleased(evt);
      }
      public void mouseExited(java.awt.event.MouseEvent evt) {
        txtAMouseExited(evt);
      }
      public void mouseEntered(java.awt.event.MouseEvent evt) {
        txtNMouseEntered(evt);
      }
    });
    txtN.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        txtNActionPerformed(evt);
      }
    });
    txtN.addFocusListener(new java.awt.event.FocusAdapter() {
      public void focusGained(java.awt.event.FocusEvent evt) {
        txtNFocusGained(evt);
      }
      public void focusLost(java.awt.event.FocusEvent evt) {
        txtNFocusLost(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 5;
    gridBagConstraints.gridy = 4;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    gridBagConstraints.insets = new java.awt.Insets(0, 1, 0, 1);
    jPanel12.add(txtN, gridBagConstraints);

    jLabel4.setLabelFor(txtTM);
    jLabel4.setText("TM");
    jLabel4.setMaximumSize(null);
    jLabel4.setMinimumSize(null);
    jLabel4.setPreferredSize(null);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 6;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.insets = new java.awt.Insets(0, 1, 0, 1);
    jPanel12.add(jLabel4, gridBagConstraints);

    txtTM.setText("TastieraMemoria");
    txtTM.setToolTipText(bundle_interfaccia.getString("Tastiera_Memoria")); // NOI18N
    txtTM.setMaximumSize(null);
    txtTM.setMinimumSize(null);
    txtTM.setPreferredSize(null);
    txtTM.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        txtTMMousePressed(evt);
      }
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        txtTMMouseReleased(evt);
      }
      public void mouseExited(java.awt.event.MouseEvent evt) {
        txtAMouseExited(evt);
      }
      public void mouseEntered(java.awt.event.MouseEvent evt) {
        txtTMMouseEntered(evt);
      }
    });
    txtTM.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        txtTMActionPerformed(evt);
      }
    });
    txtTM.addFocusListener(new java.awt.event.FocusAdapter() {
      public void focusGained(java.awt.event.FocusEvent evt) {
        txtTMFocusGained(evt);
      }
      public void focusLost(java.awt.event.FocusEvent evt) {
        txtTMFocusLost(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 7;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new java.awt.Insets(0, 1, 0, 1);
    jPanel12.add(txtTM, gridBagConstraints);

    jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
    jLabel5.setLabelFor(txtTI);
    jLabel5.setText("TI");
    jLabel5.setMaximumSize(null);
    jLabel5.setMinimumSize(null);
    jLabel5.setPreferredSize(null);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 6;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.insets = new java.awt.Insets(0, 1, 0, 1);
    jPanel12.add(jLabel5, gridBagConstraints);

    txtTI.setText("TastieraIstruzioni");
    txtTI.setToolTipText(bundle_interfaccia.getString("Tastiera_Istruzioni")); // NOI18N
    txtTI.setMaximumSize(null);
    txtTI.setMinimumSize(null);
    txtTI.setPreferredSize(null);
    txtTI.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        txtTIMousePressed(evt);
      }
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        txtTIMouseReleased(evt);
      }
      public void mouseExited(java.awt.event.MouseEvent evt) {
        txtAMouseExited(evt);
      }
      public void mouseEntered(java.awt.event.MouseEvent evt) {
        txtTIMouseEntered(evt);
      }
    });
    txtTI.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        txtTIActionPerformed(evt);
      }
    });
    txtTI.addFocusListener(new java.awt.event.FocusAdapter() {
      public void focusGained(java.awt.event.FocusEvent evt) {
        txtTIFocusGained(evt);
      }
      public void focusLost(java.awt.event.FocusEvent evt) {
        txtTIFocusLost(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 7;
    gridBagConstraints.gridy = 3;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new java.awt.Insets(0, 1, 0, 1);
    jPanel12.add(txtTI, gridBagConstraints);

    jLabel6.setLabelFor(txtVI);
    jLabel6.setText("IV");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 6;
    gridBagConstraints.gridy = 4;
    gridBagConstraints.insets = new java.awt.Insets(0, 1, 0, 1);
    jPanel12.add(jLabel6, gridBagConstraints);

    txtVI.setEditable(false);
    txtVI.setText(bundle_interfaccia.getString("Visualizzatore")); // NOI18N
    txtVI.setToolTipText("Visualizzatore");
    txtVI.setMaximumSize(null);
    txtVI.setMinimumSize(null);
    txtVI.setPreferredSize(null);
    txtVI.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        txtVIMousePressed(evt);
      }
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        txtVIMouseReleased(evt);
      }
      public void mouseExited(java.awt.event.MouseEvent evt) {
        txtAMouseExited(evt);
      }
      public void mouseEntered(java.awt.event.MouseEvent evt) {
        txtVIMouseEntered(evt);
      }
    });
    txtVI.addFocusListener(new java.awt.event.FocusAdapter() {
      public void focusGained(java.awt.event.FocusEvent evt) {
        txtVIFocusGained(evt);
      }
    });
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 7;
    gridBagConstraints.gridy = 4;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new java.awt.Insets(0, 1, 0, 1);
    jPanel12.add(txtVI, gridBagConstraints);

    jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.gridheight = 2;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    jPanel12.add(jSeparator1, gridBagConstraints);

    jSeparator5.setOrientation(javax.swing.SwingConstants.VERTICAL);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 3;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.gridheight = 5;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    jPanel12.add(jSeparator5, gridBagConstraints);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 4;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.gridwidth = 5;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new java.awt.Insets(2, 0, 0, 0);
    jPanel12.add(jSeparator4, gridBagConstraints);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    gridBagConstraints.insets = new java.awt.Insets(2, 0, 0, 0);
    jPanel12.add(jSeparator6, gridBagConstraints);

    lblClockCounter.setText("0");
    lblClockCounter.setToolTipText("0");
    lblClockCounter.setMaximumSize(null);
    lblClockCounter.setMinimumSize(null);
    lblClockCounter.setPreferredSize(null);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 8;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
    jPanel12.add(lblClockCounter, gridBagConstraints);

    lblClockLabel.setLabelFor(lblClockCounter);
    lblClockLabel.setText(bundle_interfaccia.getString("Clock_Count:_")); // NOI18N
    lblClockLabel.setToolTipText(bundle_interfaccia.getString("Clock_Count")); // NOI18N
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 7;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    jPanel12.add(lblClockLabel, gridBagConstraints);

    jPanel5.add(jPanel12, java.awt.BorderLayout.WEST);

    add(jPanel5, java.awt.BorderLayout.NORTH);

    jPanel4.setLayout(new java.awt.BorderLayout());

    jTable1.setModel(new javax.swing.table.DefaultTableModel(
      new Object [][] {
        {null, null, null, null},
        {null, null, null, null},
        {null, null, null, null},
        {null, null, null, null}
      },
      new String [] {
        "Title 1", "Title 2", "Title 3", "Title 4"
      }
    ));
    jTable1.setToolTipText(bundle_interfaccia.getString("Fare_click_col_tasto_destro_per_accedere_al_menu_contestuale")); // NOI18N
    jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
    jTable1.setAutoscrolls(false);
    jTable1.setCellSelectionEnabled(true);
    jTable1.getTableHeader().setResizingAllowed(false);
    jTable1.getTableHeader().setReorderingAllowed(false);
    jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        jTable1MousePressed(evt);
      }
      public void mouseReleased(java.awt.event.MouseEvent evt) {
        jTable1MouseReleased(evt);
      }
      public void mouseExited(java.awt.event.MouseEvent evt) {
        txtAMouseExited(evt);
      }
    });
    jTable1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
      public void mouseMoved(java.awt.event.MouseEvent evt) {
        jTable1MouseMoved(evt);
      }
    });
    jScrollPane1.setViewportView(jTable1);

    jPanel4.add(jScrollPane1, java.awt.BorderLayout.CENTER);

    jPanel23.setComponentPopupMenu(jPopupMenu1);
    jPanel23.setLayout(new javax.swing.BoxLayout(jPanel23, javax.swing.BoxLayout.Y_AXIS));

    lblSelectedLabel.setLabelFor(lblSelected);
    lblSelectedLabel.setText(bundle_interfaccia.getString("Selezionata:")); // NOI18N
    jPanel23.add(lblSelectedLabel);

    lblSelected.setText(" ");
    jPanel23.add(lblSelected);

    jSeparator10.setMaximumSize(new java.awt.Dimension(32767, 12));
    jPanel23.add(jSeparator10);

    lblMouseLabel.setLabelFor(lblMouse);
    lblMouseLabel.setText(bundle_interfaccia.getString("Sotto_il_mouse:")); // NOI18N
    jPanel23.add(lblMouseLabel);

    lblMouse.setText("<html> ");
    jPanel23.add(lblMouse);

    jPanel4.add(jPanel23, java.awt.BorderLayout.EAST);

    add(jPanel4, java.awt.BorderLayout.CENTER);
  }// </editor-fold>//GEN-END:initComponents

  private void txtTMActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTMActionPerformed
    try{
    TM=understand(txtTM.getText(), m ,TM);
    fireChange(FLAG_TM);
    _updateregisters();
    }catch(NumberFormatException e){
        e.printStackTrace();
        txtTM.setText(""+TM);
    }
}//GEN-LAST:event_txtTMActionPerformed

  private void rdAutoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdAutoActionPerformed
    mode=Macchina.AUTOMATIC_MODE;
    fireChange(FLAG_MODE);
}//GEN-LAST:event_rdAutoActionPerformed

  private void rdManualInstrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdManualInstrActionPerformed
    mode=Macchina.MANUAL_INSTR_MODE;
    fireChange(FLAG_MODE);
}//GEN-LAST:event_rdManualInstrActionPerformed

  private void rdManualMicroinstrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rdManualMicroinstrActionPerformed
    mode=Macchina.MANUAL_MICRO_MODE;
    fireChange(FLAG_MODE);
}//GEN-LAST:event_rdManualMicroinstrActionPerformed

  private void chkExternalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkExternalActionPerformed
    external=chkExternal.isSelected();
    fireChange(FLAG_EXT);
}//GEN-LAST:event_chkExternalActionPerformed

  private void chkACActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkACActionPerformed
    AC=chkAC.isSelected();
    fireChange(FLAG_AC);
}//GEN-LAST:event_chkACActionPerformed

  
/**
 * An array indexed by type containing the width of the columns of the 
 * memory display.
 */  private static final int[] widtharr={76,76,172,64,64,64,64};
 private void comboDefTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboDefTypeActionPerformed
    _updatedefaulttype();
}//GEN-LAST:event_comboDefTypeActionPerformed

  private void btnRunActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRunActionPerformed
    fireChange(FLAG_START);
}//GEN-LAST:event_btnRunActionPerformed

  private void txtTIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTIActionPerformed
      try{
      TI=(short)understand(txtTI.getText(), m ,TI);
      fireChange(FLAG_TI);
      _updateregisters();
      }catch(NumberFormatException e){
          txtTI.setText(""+TI);
      }
}//GEN-LAST:event_txtTIActionPerformed

  private void txtAMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtAMouseEntered
    _showinfo(NOME_REG_A, m.getA());
}//GEN-LAST:event_txtAMouseEntered

  private void txtAMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtAMouseExited
    _hideinfo();
}//GEN-LAST:event_txtAMouseExited

  private void txtBMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtBMouseEntered
    _showinfo(NOME_REG_B, m.getB());
}//GEN-LAST:event_txtBMouseEntered

  private void txtNMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtNMouseEntered
    _showinfo(NOME_REG_N, m.getN(),false,false);
}//GEN-LAST:event_txtNMouseEntered

  private void txtTMMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtTMMouseEntered
    _showinfo(NOME_TM, TM);
}//GEN-LAST:event_txtTMMouseEntered

  private void txtTIMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtTIMouseEntered
    _showinfo(NOME_TI, TI,false);
}//GEN-LAST:event_txtTIMouseEntered

  private void txtVIMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtVIMouseEntered
    _showinfo(NOME_VI, m.getVI());
}//GEN-LAST:event_txtVIMouseEntered

  private void txtAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtAActionPerformed
    try{
      m.setA(understand(txtA.getText(), m, m.getA()));
      txtA.setText(niceformat(m.getA(), m, 
              rcolors[~NOME_REG_A]<0?modovisualizza:rcolors[~NOME_REG_A]));
    }catch(NumberFormatException e){
      txtA.setText(""+m.getA());
    }
}//GEN-LAST:event_txtAActionPerformed

  private void txtBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtBActionPerformed
    try{
    m.setB(understand(txtB.getText(), m, m.getB()));
    txtB.setText(niceformat(m.getB(), m,
            rcolors[~NOME_REG_B]<0?modovisualizza:rcolors[~NOME_REG_B] ));
    }catch(NumberFormatException e){
        txtB.setText(""+m.getB());
    }
}//GEN-LAST:event_txtBActionPerformed

  private void txtNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNActionPerformed
    try{  
    short neun = (short)understand(txtN.getText(), m, m.getN());
    if(m.getN()==neun)return;
    int[] cols = jTable1.getSelectedColumns();
    int[] rows = jTable1.getSelectedRows();
    m.setN(neun);    
    
    updateMEM();
    
    jTable1.clearSelection();
    
    if(cols.length>0 && rows.length>0){
        jTable1.addColumnSelectionInterval(cols[0], cols[cols.length-1]);
        jTable1.addRowSelectionInterval(rows[0], rows[rows.length-1]);
    }
    }catch(NumberFormatException r){
        txtN.setText(""+m.getN());
    }
}//GEN-LAST:event_txtNActionPerformed

  private void jTable1MouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseMoved
    int y = jTable1.rowAtPoint(evt.getPoint());
    int x = jTable1.columnAtPoint(evt.getPoint());
    int nc = jTable1.getColumnCount();
    short ml =(short) (y*nc+x);
    if(ml<1024)
      _showinfo(ml,m.getMEM(ml));
    else
      _hideinfo();
  }//GEN-LAST:event_jTable1MouseMoved

  private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
    _settipo(2);
  }//GEN-LAST:event_jMenuItem4ActionPerformed

  private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
    _settipo(0);
  }//GEN-LAST:event_jMenuItem2ActionPerformed

  private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
    _settipo(1);
  }//GEN-LAST:event_jMenuItem3ActionPerformed

  private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
    _settipo(3);
  }//GEN-LAST:event_jMenuItem5ActionPerformed

  private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
    _settipo(4);
  }//GEN-LAST:event_jMenuItem6ActionPerformed

  private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
    _settipo(5);
  }//GEN-LAST:event_jMenuItem7ActionPerformed

  private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem8ActionPerformed
    _settipo(-1);
  }//GEN-LAST:event_jMenuItem8ActionPerformed

  private void jMenuItem9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem9ActionPerformed
    int c[] = jTable1.getSelectedColumns();
    int r[] = jTable1.getSelectedRows();
    if((c.length>1)||(r.length>1))return;
    if(curselected>=0){
        if(m.getN()!=curselected)
            m.setN(curselected);
        else
            return;
    }else{
        switch(curselected){
            case NOME_REG_A:
                m.setN((short)m.getA());
                break;
            case NOME_REG_B:
                m.setN((short)m.getB());
                break;
            case NOME_TI:
                m.setN(m.getTI());
                break;
            case NOME_TM:
                m.setN((short)m.getTM());
                break;
            case NOME_VI:
                m.setN((short)VI);
                break;
            default:
                return;
        }
    }
    
    updateMEM();
    
    jTable1.clearSelection();
    if(c.length>=1 && r.length>=1){
        jTable1.addColumnSelectionInterval(c[0], c[c.length-1]);
        jTable1.addRowSelectionInterval(r[0], r[r.length-1]);
    }
  }//GEN-LAST:event_jMenuItem9ActionPerformed

  private void jMenuItem10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem10ActionPerformed
    try{      
    int res=Integer.parseInt(JOptionPane.showInputDialog(
            bundle_interfaccia.getString("Indirizzo_da_selezionare:"),0).trim());
    
    int r = res / jTable1.getColumnCount();
    int c = res % jTable1.getColumnCount();
    
    jTable1.clearSelection();
    
    jTable1.addColumnSelectionInterval(c, c);
    jTable1.addRowSelectionInterval(r, r);
    
    }catch(NumberFormatException ignore){}
    catch(NullPointerException ignore){}
  }//GEN-LAST:event_jMenuItem10ActionPerformed

  private void jRadioButtonMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem1ActionPerformed
    _mktablemodel(jTable1,8);
  }//GEN-LAST:event_jRadioButtonMenuItem1ActionPerformed

  private void jRadioButtonMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem2ActionPerformed
    _mktablemodel(jTable1,10);
  }//GEN-LAST:event_jRadioButtonMenuItem2ActionPerformed

  private void jRadioButtonMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem3ActionPerformed
    _mktablemodel(jTable1,16);
  }//GEN-LAST:event_jRadioButtonMenuItem3ActionPerformed

  private void jRadioButtonMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem4ActionPerformed
    _mktablemodel(jTable1,20);
  }//GEN-LAST:event_jRadioButtonMenuItem4ActionPerformed

  private void jRadioButtonMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonMenuItem5ActionPerformed
    _mktablemodel(jTable1,32);
  }//GEN-LAST:event_jRadioButtonMenuItem5ActionPerformed

  private void txtAFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtAFocusLost
    txtAActionPerformed(null);
}//GEN-LAST:event_txtAFocusLost

  private void txtBFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtBFocusLost
    txtBActionPerformed(null);
}//GEN-LAST:event_txtBFocusLost

  private void txtNFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtNFocusLost
    txtNActionPerformed(null);
}//GEN-LAST:event_txtNFocusLost

  private void txtTMFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtTMFocusLost
    txtTMActionPerformed(null);
}//GEN-LAST:event_txtTMFocusLost

  private void txtTIFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtTIFocusLost
    txtTIActionPerformed(null);
}//GEN-LAST:event_txtTIFocusLost

  private void jCheckBoxMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxMenuItem1ActionPerformed
    showsidebar= jCheckBoxMenuItem1.isSelected();
    
    _hideinfo();     
    _updatecurselected();
  }//GEN-LAST:event_jCheckBoxMenuItem1ActionPerformed

  private void txtNFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtNFocusGained
    jTable1.clearSelection();
    curselected = NOME_REG_N;
    _updatecurselected();
}//GEN-LAST:event_txtNFocusGained

  private void txtBFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtBFocusGained
    jTable1.clearSelection();
    curselected = NOME_REG_B;
    _updatecurselected();
}//GEN-LAST:event_txtBFocusGained

  private void txtAFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtAFocusGained
    jTable1.clearSelection();
    curselected = NOME_REG_A;
    _updatecurselected();
}//GEN-LAST:event_txtAFocusGained

  private void txtTMFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtTMFocusGained
   jTable1.clearSelection();
    curselected = NOME_TM;
    _updatecurselected();
}//GEN-LAST:event_txtTMFocusGained

  private void txtTIFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtTIFocusGained
   jTable1.clearSelection();
    curselected = NOME_TI;
    _updatecurselected();
}//GEN-LAST:event_txtTIFocusGained

  private void txtVIFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtVIFocusGained
   jTable1.clearSelection();
    curselected = NOME_VI;
    _updatecurselected();
}//GEN-LAST:event_txtVIFocusGained

  private void jMenuItem11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem11ActionPerformed
    _setmemselected((short)0);
  }//GEN-LAST:event_jMenuItem11ActionPerformed

  private void jMenuItem12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem12ActionPerformed
    int v=0;
    int c[] = jTable1.getSelectedColumns();
    int r[] = jTable1.getSelectedRows();
    if(c.length<=0 && r.length <=0)return;
    
    String res = JOptionPane.showInputDialog(bundle_interfaccia.getString("Inserire_il_valore:"));
    if(res==null)return;
    v=understand(res , m, v);
    _setmemselected(v);
  }//GEN-LAST:event_jMenuItem12ActionPerformed

  private void jMenuItem13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem13ActionPerformed
    _settipo(6);
  }//GEN-LAST:event_jMenuItem13ActionPerformed

  private void txtAMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtAMousePressed
      //txtAFocusGained(null);
      txtA.requestFocusInWindow();
      if(evt.isPopupTrigger())jPopupMenu1.show(txtA, evt.getX(), evt.getY());

  }//GEN-LAST:event_txtAMousePressed

  private void txtAMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtAMouseReleased
      if(evt.isPopupTrigger())jPopupMenu1.show(txtA, evt.getX(), evt.getY());
  }//GEN-LAST:event_txtAMouseReleased

  private void txtTMMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtTMMousePressed
      //txtTMFocusGained(null);
      txtTM.requestFocusInWindow();
      if(evt.isPopupTrigger())jPopupMenu1.show(txtTM, evt.getX(), evt.getY());
  }//GEN-LAST:event_txtTMMousePressed

  private void txtTMMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtTMMouseReleased
      if(evt.isPopupTrigger())jPopupMenu1.show(txtTM, evt.getX(), evt.getY());
  }//GEN-LAST:event_txtTMMouseReleased

  private void txtBMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtBMousePressed
      //txtBFocusGained(null);
      txtB.requestFocusInWindow();
      if(evt.isPopupTrigger())jPopupMenu1.show(txtB, evt.getX(), evt.getY());
  }//GEN-LAST:event_txtBMousePressed

  private void txtBMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtBMouseReleased
      if(evt.isPopupTrigger())jPopupMenu1.show(txtB, evt.getX(), evt.getY());
  }//GEN-LAST:event_txtBMouseReleased

  private void txtTIMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtTIMousePressed
      //txtTIFocusGained(null);
      txtTI.requestFocusInWindow();
      if(evt.isPopupTrigger())jPopupMenu1.show(txtTI, evt.getX(), evt.getY());
  }//GEN-LAST:event_txtTIMousePressed

  private void txtTIMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtTIMouseReleased
      if(evt.isPopupTrigger())jPopupMenu1.show(txtTI, evt.getX(), evt.getY());
  }//GEN-LAST:event_txtTIMouseReleased

  private void txtNMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtNMousePressed
    //txtNFocusGained(null);
    txtN.requestFocusInWindow();
    if(evt.isPopupTrigger())jPopupMenu1.show(txtN, evt.getX(), evt.getY());
  }//GEN-LAST:event_txtNMousePressed

  private void txtNMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtNMouseReleased
    if(evt.isPopupTrigger())jPopupMenu1.show(txtN, evt.getX(), evt.getY());
  }//GEN-LAST:event_txtNMouseReleased

  private void txtVIMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtVIMousePressed
    //txtVIFocusGained(null);
    txtVI.requestFocusInWindow();
    if(evt.isPopupTrigger())jPopupMenu1.show(txtVI, evt.getX(), evt.getY());
  }//GEN-LAST:event_txtVIMousePressed

  private void txtVIMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtVIMouseReleased
      if(evt.isPopupTrigger())jPopupMenu1.show(txtVI, evt.getX(), evt.getY());
  }//GEN-LAST:event_txtVIMouseReleased

  private void jTable1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MousePressed
    int y = jTable1.rowAtPoint(evt.getPoint());
    int x = jTable1.columnAtPoint(evt.getPoint());
    int nc = jTable1.getColumnCount();
    short ml =(short) (y*nc+x);
    if(jTable1.getSelectedColumnCount()<=1 && jTable1.getSelectedRowCount()<=1){
        jTable1.clearSelection();
        jTable1.addColumnSelectionInterval(x, x);
        jTable1.addRowSelectionInterval(y, y);
    }
    jTable1.requestFocusInWindow();
    if(evt.isPopupTrigger())jPopupMenu1.show(jTable1, evt.getX(), evt.getY());
  }//GEN-LAST:event_jTable1MousePressed

  private void jTable1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseReleased
      if(evt.isPopupTrigger())jPopupMenu1.show(jTable1, evt.getX(), evt.getY());
  }//GEN-LAST:event_jTable1MouseReleased
  
  
  private void _setmemselected(int v){
    int c[] = jTable1.getSelectedColumns();
    int r[] = jTable1.getSelectedRows();
    int nc= jTable1.getColumnCount();
    if(c.length<=0 && r.length <=0)return;
    
    try{
    if((c[0]<0)||(r[0]<0))return;
    for(int cc:c)
      for(int rr:r)
          if(rr*nc+cc<1024)
            m.setMEM((short)(rr*nc+cc), v);
    }catch(IndexOutOfBoundsException ignore){}
    
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
       updateMEM();
      }
    });
  }
  
  /**
   * Assigns the colors to indicate the type in the memory display.
   * @param col an array indexed by type of colors to be used to 
   */
  public void setColors(int[] col){
    for(int cx=0;cx<Math.min(coltypes.length,col.length);cx++){
      coltypes[cx]=Math.abs(col[cx]+1)%7-1;
    }
    updateMEM();
  }

  /**
   * Returns a copy of the colors used in the display to indicate the type.
   * @return
   */
  public int[] getColors(){
      return coltypes.clone();
  }

  
  private JTextField _getregisterbox(int i){
      switch(i){
          case NOME_REG_A: return txtA;
          case NOME_REG_B: return txtB;
          case NOME_REG_N: return txtN;
          case NOME_TI: return txtTI;
          case NOME_TM: return txtTM;
          case NOME_VI: return txtVI;
          default: return null;
      }
  }
  
  /**
   * Changes the default memory type and updates the diaply accordingly.
   * @param t the new type.
   */
  private void _settipo(int t){
    int c[] = jTable1.getSelectedColumns();
    int r[] = jTable1.getSelectedRows();
    int nc= jTable1.getColumnCount();
    
    if(c.length<=0 && r.length <=0){
        JTextField cur = _getregisterbox(curselected);
        if(cur==null)return;
        
        rcolors[~curselected]=t;
        if(t<0||t>=colori.length){
            if(curselected!=NOME_VI)
                cur.setBackground(UIManager.getColor("TextField.background"));
            else
                cur.setBackground(UIManager.getColor("TextField.inactiveBackground"));
        }else{
            cur.setBackground(colori[t]);
        }
        _updateregisters();
        return;
    }
    
    try{
    if((c[0]<0)||(r[0]<0))return;
    for(int cc:c)
      for(int rr:r)
          if(rr*nc+cc<1024)
            coltypes[rr*nc+cc]=t;
    }catch(IndexOutOfBoundsException e){}
    updateMEM();
  }
  
  /**
   * Updates the register displays.
   */
  private void _updateregisters(){
    txtA.setText(niceformat(m.getA(), m, 
            rcolors[~NOME_REG_A]<0?modovisualizza:rcolors[~NOME_REG_A]));
    txtB.setText(niceformat(m.getB(), m, 
            rcolors[~NOME_REG_B]<0?modovisualizza:rcolors[~NOME_REG_B]));
    txtTM.setText(niceformat(TM, m,
            rcolors[~NOME_TM]<0?modovisualizza:rcolors[~NOME_TM]));        
    txtVI.setText(niceformat(VI, m, 
            rcolors[~NOME_VI]<0?modovisualizza:rcolors[~NOME_VI]));

    if(modovisualizza==1){
       txtN.setText(niceformat(m.getN(), m, 
               rcolors[~NOME_REG_N]<0?3:rcolors[~NOME_REG_N], 10));
       txtTI.setText(niceformat(TI, m, 
               rcolors[~NOME_TI]<0?3:rcolors[~NOME_TI], 15));
    }else if(modovisualizza==0){
       txtN.setText(niceformat(m.getN(), m, 
               rcolors[~NOME_REG_N]<0?3:rcolors[~NOME_REG_N], 10));
       txtTI.setText(niceformat(TI, m, 
               rcolors[~NOME_TI]<0?0:rcolors[~NOME_TI], 15)); 
    }else{
       txtN.setText(niceformat(m.getN(), m, 
               rcolors[~NOME_REG_N]<0?modovisualizza:rcolors[~NOME_REG_N], 10));
       txtTI.setText(niceformat(TI, m, 
               rcolors[~NOME_TI]<0?modovisualizza:rcolors[~NOME_TI], 15));           
    }
    
    long cc = m.getClockCount();
    lblClockCounter.setText(""+cc);
    lblClockCounter.setToolTipText(""+cc);
  }
  
  /**
   * Updates the memory display.
   */
  public void updateMEM(){
    Runnable r = new Runnable() {
        public void run() {
            _updateregisters();
            if (model != null)
                model.fireTableDataChanged();
        }
    };
    
    if(!SwingUtilities.isEventDispatchThread())
        SwingUtilities.invokeLater(r);
    else
        r.run();
  }
  
  
  /**
   * Displays and updates the info panel for the given location. Equivalent to
   * _showinfo(who,s,true,true)
   * @param who the name of the location.
   * @param s the value of the location.
   */
  private void _showinfo(int who, int s){_showinfo(who,s,true,true);}
  
  /**
   * Displays and updates the info panel for the given location. Equivalent to
   * _showinfo(who,s,flo,true)
   * @param who the name of the location.
   * @param s the value of the location.
   * @param flo indicates whether floating point representation is to be
   * displayed.
   */
  private void _showinfo(int who, int s,boolean flo){
    _showinfo(who,s,flo,true);
  }
  

  private static final int NOME_REG_A = -1;
  private static final int NOME_REG_B = -2;
  private static final int NOME_REG_N = -3;
  private static final int NOME_TM = -4;
  private static final int NOME_TI = -5;
  private static final int NOME_VI = -6;
    
  private static final String[] nomi = {bundle_interfaccia.getString("Reg._A"),
  bundle_interfaccia.getString("Reg._B"),bundle_interfaccia.getString("Reg._N"),
  bundle_interfaccia.getString("TM"), bundle_interfaccia.getString("TI"),
  bundle_interfaccia.getString("Display") };
  
  private String rtempl(int wh, int s, boolean flo, boolean instr){
      String who;
      if(wh<0){
          who=nomi[~wh];
      }else
          who="["+wh+"]";
      return ilt.replace("$who", who)
                         .replace("$base10", ""+s)
                         .replace("$base16", niceformat(s, m, 4))
                         .replace("$base2", niceformat(s, m, 2))
                         .replace("$fixed", flo?niceformat(s, m, 1):"")
                         .replace("$instr", instr?niceformat(s, m, 0):"")
                         .replace("$char",flo?niceformat(s,m,5):"");
  }
  
  private static final String ilt="<html><body><table><tr><td>"+
          bundle_interfaccia.getString("locaz.:")+"</td><td>$who</td></tr><tr><td>"+
          bundle_interfaccia.getString("base_10:")+"</td><td>$base10</td></tr><tr><td>"+
          bundle_interfaccia.getString("base_16:")+"</td><td>$base16</td></tr><tr><td>"+
          bundle_interfaccia.getString("base_2:")+"</td><td><font size=\"-3\">$base2</font></td></tr><tr><td>"+          
          bundle_interfaccia.getString("virgola<br>fissa:")+"</td><td><font size=\"-2\">$fixed</font></td></tr><tr><td>"+
          bundle_interfaccia.getString("istruzione:")+"</td><td>$instr</td></tr><tr><td>"+
          bundle_interfaccia.getString("carattere:")+"</td><td>$char</td></tr><tr><td>"+          
          "</table></body></html>";
  
  /**
   * Displays and updates the info panel for the given location.
   * @param who the name of the location.
   * @param s the value of the location.
   * @param flo indicates whether floating point representation is to be
   * displayed.
   * @param instr indicates whether instruction representation is to be 
   * displayed.
   */
  private void _showinfo(int who, int s,boolean flo,boolean instr){
    if(!showsidebar)return;
    lblMouseLabel.setVisible(true);
    lblMouse.setText(rtempl(who, s, flo, instr));
  }
  
  /**
   * Hides the info panel.
   */
  private void _hideinfo(){
    lblMouseLabel.setVisible(false);
    lblMouse.setText("");
  }
  
  
  private void _updatecurselected(){
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            int value=0;
            if(showsidebar && (curselected<1024)){
                if(curselected>=0)
                    lblSelected.setText(
                        rtempl(curselected, m.getMEM(curselected),true, true));
                else {
                    switch(~curselected){
                        case 0: value = m.getA();break;
                        case 1: value = m.getB();break;
                        case 2: value = m.getN();break;
                        case 3: value = m.getTM();break;
                        case 4: value = m.getTI();break;
                        case 5: value = m.getVI();
                    }
                    lblSelected.setText(rtempl(curselected, value, true, true));
                }
                lblSelectedLabel.setVisible(true);
            }else{
                lblSelected.setText("");
                lblSelectedLabel.setVisible(false);
            }
            jMenuItem1.setText((curselected<0?
                            nomi[~curselected]
                            :"["+curselected+"]"));
        }
    });
  }
  
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton btnRun;
  private javax.swing.ButtonGroup buttonGroup1;
  private javax.swing.JCheckBox chkAC;
  private javax.swing.JCheckBox chkExternal;
  private javax.swing.JCheckBox chkRunning;
  private javax.swing.JComboBox comboDefType;
  private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel jLabel5;
  private javax.swing.JLabel jLabel6;
  private javax.swing.JMenu jMenu1;
  private javax.swing.JMenu jMenu2;
  private javax.swing.JMenuItem jMenuItem1;
  private javax.swing.JMenuItem jMenuItem10;
  private javax.swing.JMenuItem jMenuItem11;
  private javax.swing.JMenuItem jMenuItem12;
  private javax.swing.JMenuItem jMenuItem13;
  private javax.swing.JMenuItem jMenuItem2;
  private javax.swing.JMenuItem jMenuItem3;
  private javax.swing.JMenuItem jMenuItem4;
  private javax.swing.JMenuItem jMenuItem5;
  private javax.swing.JMenuItem jMenuItem6;
  private javax.swing.JMenuItem jMenuItem7;
  private javax.swing.JMenuItem jMenuItem8;
  private javax.swing.JMenuItem jMenuItem9;
  private javax.swing.JPanel jPanel12;
  private javax.swing.JPanel jPanel23;
  private javax.swing.JPanel jPanel4;
  private javax.swing.JPanel jPanel5;
  private javax.swing.JPopupMenu jPopupMenu1;
  private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem1;
  private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem2;
  private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem3;
  private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem4;
  private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem5;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JSeparator jSeparator1;
  private javax.swing.JSeparator jSeparator10;
  private javax.swing.JSeparator jSeparator2;
  private javax.swing.JSeparator jSeparator3;
  private javax.swing.JSeparator jSeparator4;
  private javax.swing.JSeparator jSeparator5;
  private javax.swing.JSeparator jSeparator6;
  private javax.swing.JSeparator jSeparator7;
  private javax.swing.JSeparator jSeparator8;
  private javax.swing.JSeparator jSeparator9;
  private javax.swing.JTable jTable1;
  private javax.swing.JLabel lblClockCounter;
  private javax.swing.JLabel lblClockLabel;
  private javax.swing.JLabel lblMouse;
  private javax.swing.JLabel lblMouseLabel;
  private javax.swing.JLabel lblSelected;
  private javax.swing.JLabel lblSelectedLabel;
  private javax.swing.ButtonGroup ncolsButtonGroup;
  private javax.swing.JRadioButton rdAuto;
  private javax.swing.JRadioButton rdManualInstr;
  private javax.swing.JRadioButton rdManualMicroinstr;
  private javax.swing.JTextField txtA;
  private javax.swing.JTextField txtB;
  private javax.swing.JTextField txtN;
  private javax.swing.JTextField txtTI;
  private javax.swing.JTextField txtTM;
  private javax.swing.JTextField txtVI;
  // End of variables declaration//GEN-END:variables

  /**
   * Adds an ActionListener.
   * @param a the ActionListener to add.
   */
  public void addActionListener(ActionListener a) {
    ascoltatori.add(a);
  }

  /**
   * Removes the given ActionListener.
   * @param a the ActionListener to remove.
   */
  public void removeActionListener(ActionListener a) {
    ascoltatori.remove(a);
  }
  
  
  /**
   * The current default mode of the memory display.
   */
  private int modovisualizza=0;
  
  /**
   * The value of the AC switch.
   */
  private boolean AC=false;
  
  /**
   * The value of the External Instructions switch.
   */
  private boolean external=false;
  
  /**
   * The value of the mode selector.
   */
  private int mode=Macchina.MANUAL_INSTR_MODE;
  
  /**
   * The value of the VI display.
   */
  private int VI=0;
  
  
  /**
   * The value of the TM input.
   */
  private int TM=0;
  
  /**
   * The value of the TI input.
   */
  private volatile short TI=0;
  
  /**
   * Returns the value of TM.
   * @return the value of TM.
   */
  public int getTM() {
    return TM;
  }
  
  /**
   * Sets the value of TM.
   * @param a the new value for TM.
   */
  public void setTM(int a) {
    TM=a;
    txtTM.setText(niceformat(a, m, modovisualizza));
  }

  /**
   * Returns the value of TI.
   * @return the value of TI.
   */
  public short getTI() {
    return TI;
  }

  /**
   * Sets the value of TI.
   * @param i the new value for TI.
   */
  public void setTI(short i) {
    TI=i;
    txtTI.setText(niceformat(i, m, modovisualizza));
  }

  /**
   * Returns the value of AC.
   * @return the value of AC.
   */
  public boolean getAC() {
    return AC;
  }

  /**
   * Sets the value of AC.
   * @param a the new value for AC.
   */
  public void setAC(final boolean a) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        _setac(a);
      }
    });
  }
  
  /**
   * Updates the AC checkbox.
   * @param a the value to be displayed.
   */
  private void _setac(boolean a){
    AC=a;
    chkAC.setSelected(a);
  }

  /**
   * Returns the value of the External Instructions switch.
   * @return the value of the External Instructions switch.
   */
  public boolean getExternal() {
    return external;
  }

  /**
   * Sets the value of the External Instructions switch.
   * @param e the new value for the External Instructions switch.
   */
  public void setExternal(final boolean e) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        _setexternal(e);
      }
    });
  }
  
  /**
   * Updates the External Instructions checkbox.
   * @param b the value to be displayed.
   */
  private void _setexternal(boolean b){
    external=b;
    chkExternal.setSelected(b);
  }

  /**
   * Returns the current mode of operation.
   * @return the current mode of operation.
   */
  public int getMode() {
    return mode;
  }

  /**
   * Sets the mode of operation.
   * @param m the new mode of operation.
   */
  public void setMode(final int m) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        _setmode(m);
      }
    });
  }
  
  /**
   * Updates the Mode Of Operation switch.
   * @param m the new mode of operation to be displayed.
   */
  private void _setmode(int m){
    switch(m){
      case Macchina.AUTOMATIC_MODE:
        rdAuto.setSelected(true);
        mode=m;
        break;
      default:
      case Macchina.MANUAL_INSTR_MODE:
        rdManualInstr.setSelected(true);
        mode=Macchina.MANUAL_INSTR_MODE;
        break;
      case Macchina.MANUAL_MICRO_MODE:
        rdManualMicroinstr.setSelected(true);
        mode=m;
        break;
    }
  }

  /**
   * Sets a new value to be displayed on VI.
   * @param aa the new value for VI.
   */
  public void setVI(int aa) {
    VI=aa;
    comboDefType.getSelectedIndex();
  }

  
  /**
   * Variable used to stop the updater thread.
   */
  private volatile boolean cond;
  
  /**
   * 
   * @param b
   */
  public void setRUNNING(final boolean b) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        _setrunning(b);
      }
    });

    if(b&&(cond!=b)){
      cond=b;
      (new Thread(){
        public void run(){
          while(cond){
            try{
              Thread.sleep(refresh);
            }catch(InterruptedException e){}
            updateMEM();
          }
        }
      }).start();
    }
    else
      updateMEM(); // "残酷な天使のテーゼ" 
    
    cond=b;
  }
  
  /**
   * Updates the Running checkbox.
   * @param b the new value for the Running checkbox.
   */
  private void _setrunning(boolean b){
    chkRunning.setSelected(b);
  }
  
  /**
   * Returns the currently selected memory location.
   * @return the currently selected memory location.
   */
  public short getSelected(){
    return curselected;
  }
  
  public void setRefresh(int ref){
      refresh = ref;
  }
  
  public int getRefresh(){
      return refresh;
  }
  
}
